package com.huifer.wgx.controller;

import com.huifer.wgx.pojo.CommonResult;
import com.huifer.wgx.model.FunctionEntity;
import com.huifer.wgx.service.FunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title : FunctionController </p>
 * <p>Description : 职能</p>
 *
 * @author huifer
 * @date 2019-03-15
 */
@RestController
@RequestMapping("function")
@Api(tags = "FunctionController", description = "职能管理")
public class FunctionController {

    @Autowired
    private FunctionService functionService;

    private CommonResult commonResult = new CommonResult();

    @ApiOperation("创建职能")
    @PostMapping("/save")
    public ResponseEntity saveFunction(FunctionEntity function) {
        try {
            FunctionEntity functionEntity = functionService.saveFunction(function);
            return ResponseEntity.ok(commonResult.success(functionEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("查询所有职能")
    @GetMapping("/find_all")
    public ResponseEntity findAllFunction() {
        try {
            List<FunctionEntity> all = functionService.findAll();
            return ResponseEntity.ok(commonResult.success(all));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("分页查询职能")
    @GetMapping("find")
    public ResponseEntity page(
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity.ok(commonResult.success(functionService.list(pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("修改职能名称根据名称")
    @PutMapping("/edit_by_name")
    public ResponseEntity editByName(
            @RequestParam("old_function") String oldFunction,
            @RequestParam("new_function") String newFunction) {
        try {
            return ResponseEntity.ok(commonResult
                    .success(functionService.updateFunction(oldFunction, newFunction)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("修改职能名称根据id")
    @PutMapping("/edit_by_id")
    public ResponseEntity editById(
            @RequestParam("func_i_d") Long funcID,
            @RequestParam("new_function") String newFunction) {
        try {
            return ResponseEntity.ok(commonResult
                    .success(functionService.updateFunction(funcID, newFunction)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("删除职能根据职能id")
    @DeleteMapping("/remove_by_id")
    public ResponseEntity removeByID(@RequestParam("func_id") Long funcID) {
        try {
            return ResponseEntity.ok(commonResult
                    .success(functionService.deleteFunction(funcID)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("修改职能名称根据名称")
    @DeleteMapping("/remove_by_name")
    public ResponseEntity removeByName(@RequestParam("func_name") String funcName) {
        try {
            return ResponseEntity.ok(commonResult
                    .success(functionService.deleteFunction(funcName)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

}
